# -*- coding: utf-8 -*-
# @Time    : 2021/4/9 17:00
# @Author  : 夏思毅
# @Email   : 2739124228@qq.com
# @File    : 4.9.py
# @Software: PyCharm


# 生成一个ips.txt大文件，要求120000行，每行随机为172.25.254.0/24段的ip（0-255）
# 读取ips.txt文件统计这个文件中ip出现频率前10的ip
# import  random
# 定义一个表储存所有的IP地址
# list=[]
# for i in range(256):
#     list.append(f"172.25.254.{i}")
#随机添加进文档
# for i in range(50):
#     with open("ips.txt","a+") as fp1:
#             fp1.write(list[random.randint(0,(len(list)-1))]+'\n')
# dict={}
# for i in open("ips.txt"):
#     if i[11:] in dict:
#         dict[i[11:]] += 1
#     else :
#         dict[i[11:]] = 1
# # print(dict)
# 将每一个items写入列表
# items=list(dict.items())
# # 调用匿名函数 使用对应值排序
# items.sort(key=lambda x: x[1], reverse=True)
# for i in range(15):
#     a=items[i][0]
#     b=items[i][1]
#     print(f"ip:172.25.254.{a}","频率：",b)


# 访客：编写一个程序，提示用户输入其名字；用户做出响应后，将其名字写入guest.txt文件中
# while 1:
#     name = input("请输入姓名:")
#     with open("guest.txt","a+",encoding="utf-8") as op1:
#         op1.write(f"{name}\n")
#     print("录入成功")
#     yn = input("是否退出(yes退出):")
#     if yn =='yes':
#         break
#     else:
#         continue


# 生成一个100个MAC地址并写入文件中，MAC地址前6位（16进制）位01-AF-3B
# import random
# with open("MAC.txt","a+") as op2:
#     for i in range(10):
#         for i in range(3):
#             a = random.randint(0, 9)
#             b = chr(random.randint(65, 70))
#             # 利用ASCII表转换随机数
#             if i ==1:
#                 c = str(a)+b
#             elif i ==2:
#                 d = str(a)+b
#             else:
#                 e = str(a)+b
#         op2.write('01-AF-3B'+'-'+c+'-'+d+'-'+e+'\n')


#统计词频瓦尔登湖
import re,string,codecs

wordlist={}
word=""
count=1
with open("Walden.txt",encoding="utf-8") as fp:
    # 全文转换成小写并转换成str类型
    for i in fp:
        for j in i.lower():
            if j.isalpha():
                word+=j
            else:
                if word not in wordlist:
                    wordlist[word]=count
                else :
                    count+=1
    print(wordlist)
# print(fp3)
    # print(fp2)

# a = {}
# str=""
# with open("Walden.txt","r",encoding="utf-8") as f:
#     for i in f:
#         for j in i.upper():
#             if j.isalpha():
#                 str += j
#             else:
#                 if str not in a:
#                     a[str] = 1
#                     str = ""
#                 else:
#                     a[str] += 1
#                     str = ""
# a.pop('')
# print(a)
